/**
 * 
 */
package co.gov.medellin.sdep.sisdep.gestion.service;

import java.util.List;

import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.gestion.exception.SolicitudException;
import co.gov.medellin.sdep.sisdep.gestion.exception.SolicitudRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.exception.descriptor.SolicitudMessageDescriptor;
import co.gov.medellin.sdep.sisdep.model.Solicitud;
import co.gov.medellin.sdep.sisdep.model.comp.SolicitudComp;
import co.gov.medellin.sdep.sisdep.model.dao.impl.ISolicitudDAOT;

/**
 * @author Usuario
 *
 */
public class SolicitudServiceImpl extends BaseService implements ISolicitudService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private ISolicitudDAOT solicitudDAO;

	/**
	 * 
	 */
	public SolicitudServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Solicitud solicitud) throws SolicitudException {
		try{
			solicitud.setUsuarioCrea(getPrincipalUser());
			getSolicitudDAO().insert(solicitud);
		}catch (Exception e) {
			Object [] infoError = {
				solicitud.getId()
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new SolicitudException(new SolicitudMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new SolicitudException(new SolicitudMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Solicitud solicitud) throws SolicitudException {
		try{
			solicitud.setUsuarioModifica(getPrincipalUser());
			getSolicitudDAO().update(solicitud);
		}catch (Exception e) {
			Object [] infoError = {
				solicitud.getId()
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new SolicitudRuntimeException(new SolicitudMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new SolicitudRuntimeException(new SolicitudMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Solicitud solicitud) throws SolicitudException {
		try {
			solicitud.setUsuarioModifica(getPrincipalUser());
			getSolicitudDAO().delete(solicitud);
		}catch (Exception e) {
			Object [] infoError = {
				solicitud.getId()
			};
			throw new SolicitudRuntimeException(new SolicitudMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, SolicitudComp solicitud) throws SolicitudException {
		try{
			//return getSolicitudDAO().getAllSolicitudPaginated(start, max, criteria);
			return getSolicitudDAO().load(solicitud);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new SolicitudRuntimeException(new SolicitudMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final SolicitudComp get(long id) throws SolicitudException {
		try{
			return getSolicitudDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new SolicitudRuntimeException(new SolicitudMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public ISolicitudDAOT getSolicitudDAO() {
		return solicitudDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setSolicitudDAO(ISolicitudDAOT solicitudDAO) {
		this.solicitudDAO = solicitudDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
